import numpy

print("---------------- 生成数组 ----------------")
num_list = list(range(10))
num_arr = numpy.array(num_list)
print(type(num_arr))
print(num_arr.dtype)
print(num_arr)

print("----------------")
num_arr = numpy.zeros(10, dtype=numpy.int8)
print(type(num_arr))
print(num_arr.dtype)
print(num_arr)

print("----------------")
num_arr = numpy.ones((3, 2), dtype=numpy.int8)
print(num_arr)

print("----------------")
num_arr = numpy.full((3, 2), 8, dtype=numpy.int8)
print(num_arr)

print("----------------")
num_arr = numpy.zeros_like(num_arr)
print(num_arr)

print("----------------")
num_arr = numpy.random.random((3, 3))
print(num_arr)

print("----------------")
# 经常遇到
num_arr = numpy.random.randint(0, 11, (3, 3))
print(num_arr)

print("----------------")
num_arr = numpy.arange(0, 10)
print(num_arr)

print("----------------")
num_arr = numpy.linspace(0, 9, 5, dtype=numpy.int8)
print(num_arr)

print("---------------- 访问数组中的元素 ----------------")
num_arr = numpy.random.randint(0, 10, (4, 2))
print(num_arr)
print("----------------")
print(num_arr[0])
print("----------------")
print(num_arr[-1])  # 最后一个
print("----------------")
# 以下两种访问是一样的
print(num_arr[0][0])
print(num_arr[0, 0])
print("----------------")
print(num_arr[:2, :1])
print("----------------")

print("---------------- 数组的属性 ----------------")
num_arr = numpy.random.randint(0, 10, 10)
print(num_arr)
print(num_arr.ndim)
print(num_arr.shape)
print(num_arr.size)
print(num_arr.dtype)
print(num_arr.itemsize)
print(num_arr.nbytes)

print("---------------- 数组运算 ----------------")
# 每一个元素都进行这个运算
num_arr = numpy.arange(0, 10)
print(num_arr + 10)
num_arr = numpy.ones((4, 2), dtype=numpy.int32)
print(num_arr + 10)

print("---------------- 数组统计 ----------------")
num_arr = numpy.arange(0, 10)
print(numpy.sum(num_arr))
print(num_arr.sum())
print(sum(num_arr))

print("---------------- 数组比较 ----------------")
num_arr = numpy.arange(0, 10)
print(num_arr)
print(num_arr > 5)
